package com.rocsea.robot.template.impl;

import com.rocsea.robot.config.build.KettleBuildConfig;
import com.rocsea.robot.config.build.KettleBuildTemplateConfig;
import com.rocsea.robot.config.build.TemplateInfo;
import com.rocsea.robot.factory.KettleTemplateFactory;
import com.rocsea.robot.model.entity.StepAttributeDO;
import com.rocsea.robot.model.enums.KettleBuildStrategyEnum;
import com.rocsea.robot.service.KettlePackService;
import com.rocsea.robot.service.KettleTaskConfigService;
import com.rocsea.robot.template.AbstractTemplate;
import com.rocsea.robot.model.bo.KpBuildInfo;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
/**
 * 第一次走批量插入
 * @Author RocSea
 * @Date 2022/12/7
 *
 */
@Component
public class FirstBatchTemplate extends AbstractTemplate {
    @Resource
    private KettleBuildTemplateConfig templateConfig;

    @Resource
    private KettlePackService kettlePackService;

    @Resource
    private KettleTaskConfigService kettleTaskConfigService;

    @Resource
    private KettleBuildConfig buildConfig;

    public KettleBuildConfig getBuildConfig() {
        return buildConfig;
    }

    public KettlePackService getKettlePackService() {
        return kettlePackService;
    }

    public KettleBuildTemplateConfig getTemplateConfig() {
        return templateConfig;
    }

    public KettleTaskConfigService getKettleTaskConfigService() {
        return kettleTaskConfigService;
    }

    @Override
    public TemplateInfo getTemplateInfo() {
        return templateConfig.getFirstBatch();
    }

    @Override
    public String processTableInput(List<StepAttributeDO> stepAttributeDOS) throws IOException {
        return super.processTableInputWithRowMeta(stepAttributeDOS);
    }

    @Override
    public String processSqlReplace(String sql) {
        return sql.substring(sql.indexOf("FROM"));
    }

    @Override
    public String processQueryFromTableName(List<StepAttributeDO> stepAttributeDOS) throws IOException {
        return super.processTableInputWithRowMeta(stepAttributeDOS);
    }

    @Override
    public String processKettlePack() {
        String jobParamTemplate = "{\"TABLENAME\":\"%s\",\"TASKNAME\":\"%s\",\"compensation_type\":\"2\"" +
                ",\"page_size\":\"5000\",\"commit_size\":\"1000\",\"outTableName\":\"%s\"}";
        String jobCompensationParamTemplate = "{\"TABLENAME\":\"%s\",\"TASKNAME\":\"%s(task_compensation)\"" +
                ",\"compensation_type\":\"1\",\"commit_size\":\"1000\",\"outTableName\":\"%s\"}";
        KpBuildInfo kpBuildInfo = createKpBuildInfo(buildConfig.getOutDatabase());
        String jobParam = String.format(jobParamTemplate, kpBuildInfo.getTableName(),
                kpBuildInfo.getTaskName(), kpBuildInfo.getTargetTableName());
        String jobCompensationParam = String.format(jobCompensationParamTemplate, kpBuildInfo.getTableName(),
                kpBuildInfo.getTaskName(), kpBuildInfo.getTargetTableName());
        kpBuildInfo.setJobParam(jobParam);
        kpBuildInfo.setJobCompensationParam(jobCompensationParam);
        return kettlePackService.buildKettlePack(kpBuildInfo);
    }

    @Override
    public String processKettleTaskConfig() throws ParseException {
        return kettleTaskConfigService.buildKettleTaskConfig(true);
    }

    @Override
    public void afterPropertiesSet() {
        KettleTemplateFactory.register(KettleBuildStrategyEnum.FIRST_BATCH.getText(), this);
    }

}
